package com.cc2019.buke.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cc2019.buke.R;
import com.cc2019.buke.home.entry.Product2;
import com.cc2019.buke.home.entry.Product7;

import java.util.ArrayList;
import java.util.List;


public class RxAdapter extends RecyclerView.Adapter {
    private List<Product7> proList;
    private Context context;

    public RxAdapter(Context context,List<Product7> proList){
        this.context = context;
        this.proList = new ArrayList(proList);//okhttps,请求返回的集合，不具备add和remove功能
    }
    class RxHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView nameView;
        TextView priceView;
        View item_view;

        public RxHolder(@NonNull View itemView) {
            super(itemView);
            nameView = itemView.findViewById(R.id.rx_name);
            imageView = itemView.findViewById(R.id.rx_img);
            priceView = itemView.findViewById(R.id.rx_price);
            //对当前的itemView绑定监听事件
            item_view = itemView;
        }
        public void setData(Product7 pro){
            imageView.setImageResource(pro.getImgId());
            priceView.setText("$"+pro.getPrice());
            nameView.setText(pro.getName());
            priceView.setText("$"+pro.getPrice());
            item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Toast.makeText(context,"当前商品被点击,商品价格是："+priceView.getText(),Toast.LENGTH_SHORT).show();
                    onCommProClick.onItemClick(pro);
                }
            });
        }
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.home_rx_item,parent,false);
        return new RxHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Product7 product2 = proList.get(position);
        RxHolder rxHolder = (RxHolder) holder;
        rxHolder.setData(product2);

    }

    @Override
    public int getItemCount() {
        return proList.size();
    }

    //事件处理
    public interface OnCommProClick{
        public void onItemClick(Product7 product);
    }
    public RxAdapter.OnCommProClick onCommProClick;

    public void setOnCommProClick(OnCommProClick onCommProClick){
        this.onCommProClick = onCommProClick;
    }
}
